﻿using System;
using System.Collections.Generic;
using System.Text;
using Manager.App_Code.IDAL;
using Manager.App_Code.DTO;
using System.Data;

namespace Manager.App_Code.DAL
{
    class PhongDAL:StandardConnectDB, IPhong
    {


        public List<PhongDTO> GetListPhong()
        {
            List<PhongDTO> listsPhong = new List<PhongDTO>();
         
            DataTable dt = this.DTExecuteSelect("Select * from Phong");

            foreach (DataRow dr in dt.Rows)
            {
                listsPhong.Add(ConvertObjectDataRowToPhongDTODataRow(dr));
            }

            return listsPhong;
            
        }

        public DTO.PhongDTO GetPhongByID(string MaPhong)
        {
            
            PhongDTO phong = new PhongDTO();

            Dictionary<string, object> paramlist = new Dictionary<string, object>();

            paramlist.Add("@MaPhong",MaPhong);

            DataTable dt = this.DTExecuteSelect("SP_Phong_GetByID", paramlist);
            if (dt.Rows.Count > 0)
            {
                phong = ConvertObjectDataRowToPhongDTODataRow(dt.Rows[0]);

                return phong;
            }
            return null;
         
            
        }

        public List<DTO.PhongDTO> GetAllPhongByMaTang(int Matang)
        {
             List<PhongDTO> listsPhong = new List<PhongDTO>();

            Dictionary<string, object> paramlist = new Dictionary<string, object>();

            paramlist.Add("@MaTang", Matang);

            DataTable dt = this.DTExecuteSelect("SP_Phong_GetByMaTang", paramlist);


            foreach (DataRow dr in dt.Rows)
            {
                listsPhong.Add(ConvertObjectDataRowToPhongDTODataRow(dr));
            }

            return listsPhong;
        }

        public bool InsertPhong(DTO.PhongDTO ph)
        {

            Dictionary<string, object> paramlist = new Dictionary<string, object>();

            paramlist.Add("@MaPhong", ph.MaPhong);
            paramlist.Add("@MaTang", ph.MaTang.MaTang);
            paramlist.Add("@DienThoai", ph.DienThoai);
            paramlist.Add("@TenPhong", ph.TenPhong);
            paramlist.Add("@TinhTrang", ph.TinhTrang.MaTinhTrang);
            paramlist.Add("@LoaiPhong", ph.LoaiPhong.MaLoaiPhong);
            paramlist.Add("@GhiChu", ph.GhiChu);
            paramlist.Add("@ViTri", ph.Vitri);

            return this.ExecuteUpdate("SP_Phong_Insert", paramlist);
            
        }

        public bool UpdatePhong(DTO.PhongDTO ph)
        {
            Dictionary<string, object> paramlist = new Dictionary<string, object>();

            paramlist.Add("@MaPhong", ph.MaPhong);
            paramlist.Add("@MaTang", ph.MaTang.MaTang);
            paramlist.Add("@DienThoai", ph.DienThoai);
            paramlist.Add("@TenPhong", ph.TenPhong);
            paramlist.Add("@TinhTrang", ph.TinhTrang.MaTinhTrang);
            paramlist.Add("@LoaiPhong", ph.LoaiPhong.MaLoaiPhong);
            paramlist.Add("@GhiChu", ph.GhiChu);
            paramlist.Add("@ViTri", ph.Vitri);

            return this.ExecuteUpdate("SP_Phong_Update", paramlist);
            
        }

        public bool DeletePhong(DTO.PhongDTO ph)
        {
             Dictionary<string, object> paramlist = new Dictionary<string, object>();

             paramlist.Add("@MaPhong", ph.MaPhong);
            
             return this.ExecuteUpdate("SP_Phong_Delete", paramlist);
        }

        public DTO.PhongDTO ConvertObjectDataRowToPhongDTODataRow(System.Data.DataRow dr)
        {
            if (dr != null)
            {
                PhongDTO phong = new PhongDTO();

                phong.MaPhong = dr["MaPhong"].ToString();

                ITang itang = new TangDAL();

                if (dr["MaTang"].ToString() != "")
                    phong.MaTang = itang.GetTangByID(int.Parse(dr["MaTang"].ToString()));

                phong.DienThoai = dr["DienThoai"].ToString();

                phong.TenPhong = dr["TenPhong"].ToString();

                ITinhTrangPhong itinhtrang = new TinhTrangPhongDAL();

                if (dr["TinhTrang"].ToString() != "")
                    phong.TinhTrang = itinhtrang.GetTinhTrangPhongByID(dr["TinhTrang"].ToString());

                if (dr["LoaiPhong"].ToString() != "")
                {
                    ILoaiPhong iloaiphong = new LoaiPhongDAL();
                    phong.LoaiPhong = iloaiphong.GetLoaiPhongByID(dr["LoaiPhong"].ToString());
                }

                phong.GhiChu = dr["GhiChu"].ToString();

                phong.Vitri = int.Parse(dr["ViTri"].ToString());

                return phong;

            }
            else
                return null;
            
        }


        public List<PhongDTO> GetPhongByState(int TinhTrang)
        {
            List<PhongDTO> listsPhong = new List<PhongDTO>();

            Dictionary<string, object> paramlist = new Dictionary<string, object>();

            paramlist.Add("@State", TinhTrang);

            DataTable dt = this.DTExecuteSelect("SP_Phong_SelectPhongByState", paramlist);


            foreach (DataRow dr in dt.Rows)
            {
                listsPhong.Add(ConvertObjectDataRowToPhongDTODataRow(dr));
            }

            return listsPhong;
        }


        public List<PhongDTO> GetPhongByLoaiPhongAndState(string MaLoaiPhong, int TinhTrang)
        {
            List<PhongDTO> listsPhong = new List<PhongDTO>();

            Dictionary<string, object> paramlist = new Dictionary<string, object>();

            paramlist.Add("@TinhTrang", TinhTrang);

            paramlist.Add("@LoaiPhong", MaLoaiPhong);

            DataTable dt = this.DTExecuteSelect("SP_Phong_GetByLoaiPhongAndState", paramlist);


            foreach (DataRow dr in dt.Rows)
            {
                listsPhong.Add(ConvertObjectDataRowToPhongDTODataRow(dr));
            }

            return listsPhong;
        }
    }
}
